/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.data.bean.base;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.ouertech.android.agm.lib.base.bean.BaseBean;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/8.
 * desc    : QQ access token
 */
public class QQAccessToken extends BaseBean {
    private static final long serialVersionUID = 1L;

    private static final String PREFERENCES_NAME    = "qq_access_token";

    public static final String KEY_UID              = "uid";
    public static final String KEY_ACCESS_TOKEN     = "access_token";
    public static final String KEY_EXPIRES_IN       = "expires_in";
    public static final String KEY_OPENID           = "openid";
    public static final String KEY_APPID            = "appid";

    //用户id
    private String uid;
    //token
    private String accessToken;
    //
    private String openId;
    //
    private String appId;
    //
    private long expiresTime;

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getOpenId() {
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public void setExpiresIn(String expiresIn) {
        if(!TextUtils.isEmpty(expiresIn) && !expiresIn.equals("0")) {
            this.setExpiresTime(System.currentTimeMillis() + Long.parseLong(expiresIn) * 1000L);
        }
    }

    public long getExpiresTime() {
        return expiresTime;
    }

    public void setExpiresTime(long expiresTime) {
        this.expiresTime = expiresTime;
    }

    public boolean isSessionValid() {
        return !TextUtils.isEmpty(accessToken);
    }

    public boolean isExpires() {
        return System.currentTimeMillis() - expiresTime >= 0;
    }


    /**
     * 保存 Token 对象到 SharedPreferences。
     *
     * @param context 应用程序上下文环境
     * @param token   Token 对象
     */
    public static void writeAccessToken(Context context, QQAccessToken token) {
        if (null == context || null == token) {
            return;
        }

        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(KEY_UID, token.getUid());
        editor.putString(KEY_ACCESS_TOKEN, token.getAccessToken());
        editor.putString(KEY_OPENID, token.getOpenId());
        editor.putString(KEY_APPID, token.getAppId());
        editor.putLong(KEY_EXPIRES_IN, token.getExpiresTime());
        editor.commit();
    }

    /**
     * 从 SharedPreferences 读取 Token 信息。
     *
     * @param context 应用程序上下文环境
     *
     * @return 返回 Token 对象
     */
    public static QQAccessToken readAccessToken(Context context) {
        if (null == context) {
            return null;
        }

        QQAccessToken token = new QQAccessToken();
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
        token.setUid(pref.getString(KEY_UID, ""));
        token.setAccessToken(pref.getString(KEY_ACCESS_TOKEN, ""));
        token.setOpenId(pref.getString(KEY_OPENID, ""));
        token.setAppId(pref.getString(KEY_APPID, ""));
        token.setExpiresTime(pref.getLong(KEY_EXPIRES_IN, 0));

        return token;
    }

    /**
     * 清空 SharedPreferences 中 Token信息。
     *
     * @param context 应用程序上下文环境
     */
    public static void clear(Context context) {
        if (null == context) {
            return;
        }

        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
        SharedPreferences.Editor editor = pref.edit();
        editor.clear();
        editor.commit();
    }
}
